using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MattGriffith.Windows.Forms;

using BetterEventViewer.Core;

namespace BetterEventViewer.Gui
{
    public partial class MainForm : Form
    {        
        public MainForm()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        { 
            LoadLogTree();
        }

        void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                MattGriffith.Windows.Forms.NotifyIcon icon = new MattGriffith.Windows.Forms.NotifyIcon();
                icon.Text = "Hi";
                
                icon.ShowBalloon(MattGriffith.Interop.BalloonIconStyle.Info, "Hi", "Hi");
                this.Close();
            }
        }

        
        
        private void LoadLogTree()
        {
            logTreeView.Nodes.Clear();

            BetterEventLogStore store = new BetterEventLogStore();

            foreach (BetterEventLog log in store.BetterEventLogs)
            {
                TreeNode logNode = new TreeNode();
              
                logNode.Text = log.Name;
                
                //Font font = new Font("Arial", 9, FontStyle.Bold);
                //logNode.NodeFont = font;
                                
                foreach (BetterEventLogEntry entry in log.BetterEventLogEntries)
                {
                    TreeNode entryNode = new TreeNode();

                    int stop = entry.Message.Length > 50 ? 50 : entry.Message.Length;

                    entryNode.Text = entry.Message.Substring(0,stop).Replace(System.Environment.NewLine.ToString()," ").Replace("\t"," ");
                    entryNode.ToolTipText = entry.Message;
                    logNode.Nodes.Add(entryNode);
                }

                logTreeView.Nodes.Add(logNode);
            }
        }

        private void clearEntriesButton_Click(object sender, EventArgs e)
        {
            BetterEventLogStore store = new BetterEventLogStore();
            store.ClearAllEntries();
            
            LoadLogTree();
        }
                
        private void logTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (logTreeView.SelectedNode.Level == 0)
            {
                detailsRichTextBox.Visible = false;
            }
            else
            {
                detailsRichTextBox.Visible = true;
                detailsRichTextBox.Text = logTreeView.SelectedNode.ToolTipText;
            }
            
        }

        private void createLogButton_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(logNameTextBox.Text))
            {
                BetterEventLogStore store = new BetterEventLogStore();
                BetterEventLog log = store.CreateBetterEventLog(logNameTextBox.Text);
                
                LoadLogTree();
            }
        }

        private void deleteLogsButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?","",MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            BetterEventLogStore store = new BetterEventLogStore();
            store.DeleteLogs();
            LoadLogTree();            
        }

        private void deleteSelectedLogButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }

            string name = logTreeView.SelectedNode.Text;
            BetterEventLogStore store = new BetterEventLogStore();
            BetterEventLog log = store.GetBetterEventLogByName(name);
            log.Delete();
            LoadLogTree();
        } 
    }
}